import requests
from bs4 import BeautifulSoup
import json
"""
    str转化为json
"""
def getHtml():
    user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
    head = {"User_Aget": user_agent}
    r = requests.get("http://seputu.com/", headers=head)
    return r.text
def parseHtml(text):
    content = []
    soup = BeautifulSoup(text, 'html.parser')
    mulu = soup.find_all(class_='mulu')
    for m in mulu:
        h2 =m.find('h2')
        if h2:
            h2_title = h2.string
            list = []
            for a in m.find(class_='box').find_all('a'):
                box_href = a.get('href')
                box_title = a.get('title')
                list.append({'href': box_href, 'title': box_title})
            content.append({'title': h2_title, 'content': list})
    return content

if  __name__ == '__main__':
    text = getHtml()
    content = parseHtml(text)
    with open('raiders.json', 'w') as fp:
        json.dump(content, fp=fp, indent=4)